<?php
namespace Anls\Com\Strategy;
use Anls\Common\AbstractCommonBaseController;
use Anls\Common;
/**
 * 基础抽象支持类
 * @author w.
 *
 */
abstract class AbstractStrategySupport extends AbstractCommonBaseController{
	
    public function _initialize(){
        parent::_initialize();
    }
    
	// 交易品种
	protected $symbolType = 'stock';
	// 统计周期
	protected $period = '1day';
	// 交易类型
	protected $tradeType = 'buy';
	// 开仓策略
	protected $openStrategyExpr = '';
	// 平仓策略
	protected $closeStrategyExpr = '';
	// 品种代码
	protected $code = '';
	// K线
	protected $bar = null;
	// 多开仓标志
	protected $buyFlag = false;
	// 多开仓价
	protected $buyOpenPrice = 0;
	// 多开仓日期
	protected $buyOpenDate = null;
	// 多仓平仓日期
	protected $buyCloseDate = null;
	// 多平仓价
	protected $buyClosePrice = 0;
	// 多单次盈利点数
	protected $buyWinPoint = 0;
    // 多最大盈利
	protected $buyMaxCloseProfit = 0;
    // 多最高盈利
    protected $buyMaxHighProfit = 0;
    // 多单词亏损点数
    protected $buyLossPoint = 0;
    // 多最大亏损
    protected $buyMaxCloseLoss = 0;
    // 多最高亏损
    protected $buyMaxHighLoss = 0;
    // 多仓交易日志
    protected $buyTradeLog = array();
    // 多仓交易分析
    protected $buyTradeAnls = array();
    
    protected function getBuyTradeCacheKey($code){
        $_code = isset($code)?$code:$this->code;
        return Common\Redis::$TRADE_LOG_KEY . $_code;
    }
    
    protected function getCodeKey(){
        return Common\Redis::$TRADE_CODE_KEY;
    }
    
    protected function getCodeAnlsCacheKey($code){
        $_code = isset($code)?$code:$this->code;
        return Common\Redis::$TRADE_ANLS_KEY . $_code; 
    }
    
    /**
     * 设置多仓交易数据
     * @param array $bar
     * @param int $shift
     */
    protected function setBuyData($bar,$shift){
    	$this->buyOpenPrice = $bar[$shift]['close'];
		$this->buyOpenDate = $bar[$shift]['date'];
		$this->buyMaxCloseProfit = round(max($bar[$shift]['close'] - $this->buyOpenPrice,$this->buyMaxCloseProfit),2);
		$this->buyMaxHighProfit = round(max($bar[$shift]['high'] - $this->buyOpenPrice,$this->buyMaxHighProfit),2);
		$this->buyMaxHighLoss = round(min($bar[$shift]['low']- $this->buyOpenPrice,$this->buyMaxHighLoss),2);
		$this->buyMaxCloseLoss = round(min($bar[$shift]['close']- $this->buyOpenPrice,$this->buyMaxCloseLoss),2);
		$this->buyFlag = true;
    }
    
    /**
     * 多仓开仓后数据统计
     * 
     * @param array $bar
     * @param int $shift
     */
    protected function statsBuyOpenData($bar,$shift){
    	$this->buyMaxCloseProfit = round(max($bar[$shift]['close'] - $this->buyOpenPrice,$this->buyMaxCloseProfit),2);
		$this->buyMaxHighProfit = round(max($bar[$shift]['high'] - $this->buyOpenPrice,$this->buyMaxHighProfit),2);
	
		$this->buyMaxHighLoss = round(min($bar[$shift]['low']- $this->buyOpenPrice,$this->buyMaxHighLoss),2); 
		$this->buyMaxCloseLoss = round(min($bar[$shift]['close'] - $this->buyOpenPrice,$this->buyMaxCloseLoss),2); 
    }
    
    private function setBuyTradeLog($bar){
        // 最大盈利百分比
        $buyMaxHighProfitPercent = (($this->buyOpenPrice + $this->buyMaxHighProfit) - $this->buyOpenPrice) / $this->buyOpenPrice;
        $buyMaxHighProfitPercent = round($buyMaxHighProfitPercent * 100,2);
        // 盈利百分比
        $buyWinPointPercent = ($this->buyClosePrice - $this->buyOpenPrice ) / $this->buyOpenPrice;
        $buyWinPointPercent = round($buyWinPointPercent * 100,2);
        // 最大收盘盈利百分比
        $buyMaxCloseProfitPercent = (($this->buyOpenPrice + $this->buyMaxCloseProfit) - $this->buyOpenPrice) / $this->buyOpenPrice;
        $buyMaxCloseProfitPercent = round($buyMaxCloseProfitPercent * 100,2);
        // 最大收盘亏损百分比
        $buyMaxCloseLossPercent = (($this->buyOpenPrice + $this->buyMaxCloseLoss) - $this->buyOpenPrice) / $this->buyOpenPrice;
        $buyMaxCloseLossPercent = round($buyMaxCloseLossPercent * 100,2);
        // 最大最低价亏损百分比
        $buyMaxHighLossPercent = (($this->buyOpenPrice + $this->buyMaxHighLoss) - $this->buyOpenPrice) / $this->buyOpenPrice;
        $buyMaxHighLossPercent = round($buyMaxHighLossPercent * 100,2);
        $data = array(
            'buyOpenPrice'=>$this->buyOpenPrice
            ,'buyClosePrice'=>$this->buyClosePrice
            
            ,'buyWinPoint'=>$this->buyWinPoint
            ,'bwp'=>$buyWinPointPercent
            
            ,'buyMaxCloseProfit'=>$this->buyMaxCloseProfit
            ,'bmcp'=>$buyMaxCloseProfitPercent
            
            ,'buyMaxCloseLoss'=>$this->buyMaxCloseLoss
            ,'bmcl'=>$buyMaxCloseLossPercent
            
            ,'buyOpenDate'=>$this->buyOpenDate
            ,'buyCloseDate'=>$this->buyCloseDate
            
            ,'buyMaxHighProfit'=>$this->buyMaxHighProfit
            ,'bmhp'=>$buyMaxHighProfitPercent
            
            ,'buyMaxHighLoss'=>$this->buyMaxHighLoss
            ,'bmhl'=>$buyMaxHighLossPercent
        );
        $this->redis->rPush($this->getBuyTradeCacheKey(),json_encode($data));
    	array_push($this->buyTradeLog, $data);
    }

    protected function getBuyTradeLog($code){
        if(isset($code)){
            $key = Common\Redis::$TRADE_LOG_KEY.$code;
            $logs = $this->redis->lRange($key, 0, -1);
            return $logs;
        } else {
            return $this->buyTradeLog;
        }
    }
    
    protected function getBuyTradeLogByCode($code){
        
    }
    
    /**
     * 多仓平仓数据统计
     * 
     * @param array $bar
     * @param int $shift
     */
    protected function statsBuyCloseData($bar,$shift){
    	$this->buyCloseDate = $bar[$shift]['date'];
    	$this->buyClosePrice = $bar[$shift]['close'];
    	// 单次盈利点数
		$this->buyWinPoint = round($bar[$shift]['close'] - $this->buyOpenPrice,2);
		// 设置多仓交易日志内容
		$this->setBuyTradeLog($bar);
	 	
	    // 重设多仓变量值 
		$this->buyFlag = false;
		$this->buyOpenPrice = 0;
		$this->buyWinPoint = 0;
		$this->buyLossPoint = 0;
		$this->buyMaxCloseProfit = 0;
		$this->buyMaxCloseLoss = 0;
		$this->buyMaxHighProfit = 0;
		$this->buyMaxHighLoss = 0;
		$this->buyOpenDate = null;
		$this->buyCloseDate = null;
    }
    
    protected function writeTradeLog(){
    	$isWriteFile = true;

		if($isWriteFile){
			$filePath = "c:\\trade_log\\".$this->symbolType."\\".$this->period."\\_".$this->code;
			$setBuyFilePath = $filePath . "_buy.txt";
			//$setSellFilePath = $filePath . "_sell.txt";
			$this->mk_dir(dirname($setBuyFilePath));
			$this->fileWrite($setBuyFilePath,$this->buyTradeLog);
			//fileWrite($setSellFilePath,$sellFileTxt);
			if($fi==0){
				echo '写入文件：'.$setBuyFilePath.'</br>';
				//secho '写入文件：'.$setSellFilePath.'</br>';
			}
		}
    }
    
    /**
     * 写入文件
     * 
     * @param string $filePath
     * @param string $content
     */
	private function fileWrite($filePath,$content){
		$file_pointer = fopen($filePath,"w+");     
		fwrite($file_pointer,$content);
		fclose($file_pointer);
	}
    
	/**
	 * 新建目录
	 * 
	 * @param string $path
	 */
	private function mk_dir($path){ 
		if (!file_exists($path)){ 
			$this->mk_dir(dirname($path)); 
			mkdir($path, 0777); 
		} 
	} 
}

?>
